﻿using AutoMapper;
using BBICMS.Application.InputModels.Works;
using BBICMS.Application.ViewModels;
using BBICMS.Model;
using BBICMS.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    public class ClientController : BaseController
    {
        // GET: Admin/Client
        public ActionResult Index()
        {
            IEnumerable<Client> clientList = _unit.Clients.GetAll().OrderByDescending(w => w.Importance).ThenByDescending(w => w.AddedDate);
            return View(clientList);
        }
       
        // GET: Admin/Client/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Admin/Client/Create
        public ActionResult Add()
        {
            var vClient = new ClientIM
            {
                Active = true,
                Importance = 0
            };

            return View("Add", vClient);
        }

        // POST: Admin/Client/Create
        [HttpPost]
        public ActionResult Add(ClientIM clientIM)
        {
            if (ModelState.IsValid)
            {
                var client = Mapper.Map<Client>(clientIM);
                _unit.Clients.Add(client);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Client));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertCreateFailure, EntityNames.Client));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }

        public ActionResult Edit(int id)
        {
            var vType = _unit.Clients.GetById(id);


            if (vType != null)
            {
                var client = Mapper.Map<ClientIM>(vType);
                return View(client);
            }
            return View("NotFound");
        }

        [HttpPost]

        public JsonResult Edit(ClientIM clientIM)
        {


            //site.UpdatedBy = User.Identity.Name;
            //site.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                var client = Mapper.Map<Client>(clientIM);
                client.UpdatedDate = DateTime.Now;

                _unit.Clients.Update(client);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Client));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Client));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        // DELETE: /User/DeleteSite
        [HttpPost]
        public JsonResult Delete(int id)
        {

            Client vType = _unit.Clients.GetById(id);

            if (vType != null)
            {
                //string imgurl = Server.MapPath(vBlog.ImageURL);
                //if (System.IO.File.Exists(imgurl))
                //{
                //    System.IO.File.Delete(imgurl);    //删除缩略图
                //}


                _unit.Clients.Delete(vType);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertDeleteSuccess, EntityNames.Client));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertDeleteFailure, EntityNames.Client));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }

    }
}
